package com.ruoyi.web.controller.shop.mapper;


import com.ruoyi.common.core.dao.BaseMapper;
import com.ruoyi.common.entity.DO.shop.ShopDO;
import com.ruoyi.common.entity.VO.shop.ShopSearchVO;
import com.ruoyi.common.entity.VO.shop.ShopVO;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.math.BigDecimal;
import java.util.List;


public interface IBackShopMapper extends BaseMapper<ShopDO> {

    @Select("SELECT balance FROM shop WHERE id=#{id}")
    BigDecimal getShopBalance(Long id);

    @Select("SELECT parent_id FROM shop WHERE id=#{childId}")
    Long getParentIdByChildId(Long childId);

    List<ShopVO> searchList(ShopSearchVO shopSearchVO);

    @Select("SELECT id FROM shop WHERE is_delete=0 and is_enable=1 and review_status=1")
    List<Long> searchIdList();

    @Select("SELECT id FROM shop WHERE parent_id=#{parentId} AND is_delete=0")
    List<Long> searchChildIdList(Long parentId);

    /**
     * 根据店铺ID查询信息
     *
     * @param shopId 店铺
     * @return 店铺信息
     */
    ShopVO getShopInfoById(Long shopId);

    /**
     * 根据店铺小程序appID查询信息
     *
     * @param appId 店铺appId
     * @return 店铺信息
     */
    @Select("SELECT s.id,s.shop_name shopName,s.shop_code shopCode,s.review_status reviewStatus FROM shop_applet sa left join shop s on sa.shop_id=s.id WHERE sa.applet_app_id=#{appId}")
    ShopVO getShopInfoByAppId(String appId);

    /**
     * 删除店铺
     *
     * @param shopId 店铺ID
     * @return 结果
     */
    @Update("UPDATE shop SET is_delete = 1 WHERE id = #{id}")
    int deleteShopById(Long shopId);

    @Select("SELECT COUNT(0) FROM shop WHERE id=#{id} and review_status=1 and is_delete=0")
    int checkShopIsReview(Long id);

    /**
     * 根据店铺名称查询店铺ID
     */
    @Select("SELECT id FROM shop WHERE shop_name=#{shopName} AND is_delete=0")
    Long getIdByShopName(String shopName);

    /**
     * 根据店铺昵称查询店铺ID
     */
    @Select("SELECT id FROM shop WHERE shop_code=#{shopCode} AND is_delete=0")
    Long getIdByShopCode(String shopCode);

    @Select("SELECT COUNT(*) FROM shop where review_status =1 and is_delete=0 and is_enable=1 and DATE_FORMAT(create_time, '%Y-%m')=DATE_FORMAT(NOW(), '%Y-%m')")
    int countShopCurrentMonth();

    @Select("SELECT COUNT(*) FROM shop where review_status =1 and is_delete=0 and is_enable=1 and DATE_FORMAT(create_time, '%Y-%m-%d') >= DATE_FORMAT(#{startDay}, '%Y-%m-%d') and DATE_FORMAT(create_time, '%Y-%m-%d')<=DATE_FORMAT(#{endDay}, '%Y-%m-%d')")
    int countShopBetweenDay(@Param("startDay") String startDay, @Param("endDay") String endDay);

}